import 'package:enterprise_v6_app/app/core/core.dart';

///安全措施编辑弹窗
///[text] 传入的字段
///[onCallback] 完成回调
class MeasureDynamicEditable extends StatefulWidget {
  const MeasureDynamicEditable({required this.text, required this.onCallback, super.key});

  final String text;
  final Function(String) onCallback;

  @override
  State<MeasureDynamicEditable> createState() => _MeasureDynamicEditableState();
}

class _MeasureDynamicEditableState extends State<MeasureDynamicEditable> {
  List<TextEditingController> controllers = [];

  @override
  void initState() {
    super.initState();
    // 查询字符中的'-^'和'+^'之间可以编辑的数据
    final matches = RegExp(r'-\^([^\+]+)?\+\^').allMatches(widget.text);
    for (var match in matches) {
      final String initialValue = match.group(1) ?? '';
      controllers.add(TextEditingController(text: initialValue));
    }
  }

  String getUpdatedText() {
    final List<String> parts = widget.text.split(RegExp(r'-\^([^\+]+)?\+\^'));
    final StringBuffer updatedText = StringBuffer();

    for (int i = 0; i < parts.length; i++) {
      updatedText.write(parts[i]);
      if (i < controllers.length) {
        updatedText.write('-^');
        updatedText.write(controllers[i].text);
        updatedText.write('+^');
      }
    }
    return updatedText.toString();
  }

  @override
  Widget build(BuildContext context) {
    // 展示的字段
    final List<InlineSpan> spans = [];
    final List<String> parts = widget.text.split(RegExp(r'-\^([^\+]+)?\+\^'));
    for (int i = 0; i < parts.length; i++) {
      spans.add(TextSpan(text: parts[i]));
      if (i < controllers.length) {
        spans.add(WidgetSpan(
          child: TextField(
            controller: controllers[i],
            style: TextStyle(color: Colors.red, fontWeight: FontWeight.w400, fontSize: 14.w),
            decoration: InputDecoration(
              counterText: '',
              //防止适配不居中
              isCollapsed: true,
              border: const OutlineInputBorder(borderSide: BorderSide.none),
              contentPadding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 9.w),
              hintText: '请输入',
              hintStyle: TextStyle(
                  fontSize: 14.w, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
            ),
          ),
        ));
      }
    }

    return Container(
      alignment: Alignment.center,
      child: Container(
        width: 300.w,
        height: 250.w,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(20.w)),
          gradient: const LinearGradient(
              //渐变位置
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              //渐变颜色[始点颜色, 结束颜色]
              colors: [
                Color(0xffE7EAFF),
                Color(0xffffffff),
                Color(0xffffffff),
                Color(0xffffffff),
              ]),
        ),
        child: Stack(
          alignment: Alignment.center,
          children: [
            Container(
                padding: EdgeInsets.symmetric(horizontal: 20.w),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    AppGaps.vGap20,
                    Expanded(
                        child: Container(
                      decoration: BoxDecoration(
                        color: const Color(0xffF7F7FB),
                        borderRadius: BorderRadius.all(Radius.circular(20.w)),
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 10.w),
                      child: ListView(
                        children: [
                          RichText(
                            text: TextSpan(
                              style: DefaultTextStyle.of(context).style,
                              children: spans,
                            ),
                          )
                        ],
                      ),
                    )),
                    AppGaps.vGap20,
                    SizedBox(
                      width: 270.w,
                      height: 40.w,
                      child: ElevatedButton(
                        style: ButtonStyle(
                          backgroundColor: MaterialStateProperty.all(const Color(0xff5C6DFF)),
                        ),
                        child: Text(
                          '确定',
                          style: TextStyle(fontSize: 16.sp, color: Colors.white),
                        ),
                        onPressed: () {
                          widget.onCallback(getUpdatedText());
                          Get.back();
                        },
                      ),
                    ),
                    AppGaps.vGap10,
                  ],
                )),
            Positioned(
                top: 4.w,
                right: 4.w,
                child: IconButton(
                  icon: Container(
                    width: 30.w,
                    height: 30.w,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(15.w)),
                    ),
                    child: Icon(
                      Icons.close,
                      color: Colors.grey,
                      size: 21.w,
                    ),
                  ),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ))
          ],
        ),
      ),
    );
  }
}
